from __future__ import print_function, division
import time

from matplotlib import rcParams
import matplotlib.pyplot as plt

from nilmtk import DataSet, TimeFrame, MeterGroup, HDFDataStore

def _normalize(chunk, mmax):
    '''Normalizes timeseries
    Parameters
    ----------
    chunk : the timeseries to normalize
    max : max value of the powerseries
    Returns: Normalized timeseries
    '''
    tchunk = chunk / mmax
    return tchunk


print("========== OPEN DATASETS ============")
train = DataSet('/home/i/codes/nilm/redd/dataset/redd.h5')
test = DataSet('/home/i/codes/nilm/redd/dataset/redd.h5')

train.set_window(end="4-30-2011")
test.set_window(start="4-30-2011")

window_size = 100
train_building = 1
test_building = 1
sample_period = 6
meters = ['microwave','fridge','dish washer','washer dryer']
meter_key = meters[2]
train_elec = train.buildings[train_building].elec
test_elec = test.buildings[test_building].elec

train_meter = train_elec.submeters()[meter_key]
# print(train_elec.submeters())
train_mains = train_elec.mains()
test_mains = test_elec.mains()
test_meter = test_elec.submeters()[meter_key]
# print(test_elec.submeters())

# Here we get a generator back...
train_main_power_series = train_mains.power_series(sample_period=sample_period)
train_meter_power_series = train_meter.power_series(sample_period=sample_period)
test_mains_power_series = test_mains.power_series(sample_period=sample_period)
test_meter_power_series = test_meter.power_series(sample_period=sample_period)
# There is only one chunk of data...so next() retrieves it all.1
# We are returned Pandas dataframes.
df_main = next(train_main_power_series)
df_meter = next(train_meter_power_series)
df_test_main = next(test_mains_power_series)
df_test_meter = next(test_meter_power_series)
#

# df_main.to_pickle('created_data/REDD/train_main.pkl.zip', compression='zip')
df_meter.to_pickle('created_data/REDD/train_{}.pkl.zip'.format(meter_key), compression='zip')
# df_test_main.to_pickle('created_data/REDD/test_main.pkl.zip', compression='zip')
df_test_meter.to_pickle('created_data/REDD/test_{}.pkl.zip'.format(meter_key), compression='zip')

    